package es.aa.android.cartomatic.appview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.appview.listener.DragListener;
import es.aa.android.cartomatic.appview.listener.ClickListener;
import es.aa.android.cartomatic.customview.CardView;
import es.aa.android.cartomatic.customview.CountOptionView;
import es.aa.android.cartomatic.domain.Card;
import es.aa.android.cartomatic.domain.Deck;
import es.aa.android.cartomatic.domain.Options;
import es.aa.android.cartomatic.implementations.service.BoardService;

public class BoardView extends FragmentActivity {

	private ScrollView scrollThresh;
	private LinearLayout thresh;
	private LinearLayout deck;
	private LinearLayout board;
	private List<List<Card>> boardList;
	private HorizontalScrollView handScroll;
	private LinearLayout hand;
	private List<Card> handList;
	private LinearLayout boardOptions;
	private Button nextTurn;
	
	private boolean nextPlayer;
	private Options options;
	private BoardService boardService;
	
	private int currentPlayer = 0;
	private int numPlayers;
	private Map<String, List<View>> handsPlayers;
	private Map<String, List<View>> decksPlayers;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.board_view);
		
		init();
	}

	private void init() {
		
		getActionBar().hide();
		initDeck();
		initHand();
		
		initBoard();
		createRow();
		createRow();
		
		addOptionView(new CountOptionView(this));
		loadDecksAndHands();
		
	}
	
	private void initBoard() {
		scrollThresh = (ScrollView) findViewById(R.id.scrollThresh);
		thresh = (LinearLayout) findViewById(R.id.layoutBoardThresh);
		scrollThresh.setOnDragListener(new DragListener(thresh));
		
		board = (LinearLayout) findViewById(R.id.llBoard);
		boardOptions = (LinearLayout) findViewById(R.id.layoutBoardOptions);
		
		nextTurn = (Button) findViewById(R.id.nextTurn);
		nextTurn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				next();
			}
		});
	}
	
	private void initHand() {
		handScroll = (HorizontalScrollView) findViewById(R.id.horizontalScrollHand);
		hand = (LinearLayout) findViewById(R.id.llHandCards);
		handList = new ArrayList<Card>();
		handScroll.setOnDragListener(new DragListener(hand));
	}
	
	private void initDeck() {
		HorizontalScrollView deckContainer = (HorizontalScrollView) findViewById(R.id.containerDeck);
		deck = (LinearLayout) findViewById(R.id.layoutBoardDeck);
		deckContainer.setOnDragListener(new DragListener(deck));
		
		deckContainer.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		
		deckContainer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
	}
	
	private void createRow() {

		LinearLayout newRow = new LinearLayout(this);
		newRow.setOrientation(LinearLayout.HORIZONTAL);
		newRow.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		// TODO esto tiene que ir en funcion del numero de columnas que tenga la mesa
		for (int i = 0; i < 4; i++) {
			Card emptyCard = new Card();
			emptyCard.setName("HUECO");
			CardView emptySpace = new CardView(this, emptyCard);
			emptySpace.setOnDragListener(new DragListener(emptySpace));
			newRow.addView(emptySpace);
		}
		
		board.addView(newRow, board.getChildCount(), newRow.getLayoutParams());
	}
	
	private void addOptionView (View view) {
		boardOptions.addView(view);
	}
	
	private void next() {
		
		if(nextPlayer) {
			nextTurn.setText("Pasar turno");
			nextPlayer = false;
			deck.setVisibility(View.VISIBLE);
			hand.setVisibility(View.VISIBLE);
			
			
			if(numPlayers == currentPlayer) {
				currentPlayer = 0;
			} else {
				currentPlayer++;
			}
			Log.i("PLAYER", String.valueOf(currentPlayer));
			loadTurn(currentPlayer);
			
		} else {
			nextTurn.setText("Empezar turno");
			nextPlayer = true;
			deck.setVisibility(View.INVISIBLE);
			hand.setVisibility(View.INVISIBLE);
			
			saveTurn(currentPlayer);
		}
	}
	
	private void loadTurn(int numPlayer) {

		List<View> tempListHand = handsPlayers.get(String.valueOf(numPlayer) + "hand");
		List<View> tempListDeck = decksPlayers.get(String.valueOf(numPlayer) + "deck");
		
		if(tempListHand != null) {
			for (int i = 0; i < tempListHand.size(); i++) {
				hand.addView(tempListHand.get(i));
			}
		}
		
		if(tempListDeck != null) {
			for (int i = 0; i < tempListDeck.size(); i++) {
				deck.addView(tempListDeck.get(i));
			}	
		}
	
	}
	
	private void saveTurn(int numPlayer) {

		List<View> tempListHand = new ArrayList<View>();
		List<View> tempListDeck = new ArrayList<View>();
		
		for (int i = 0; i < hand.getChildCount(); i++) {
			tempListHand.add(hand.getChildAt(i));
		}
		
		for (int i = 0; i < deck.getChildCount(); i++) {
			tempListDeck.add(deck.getChildAt(i));
		}
		
		handsPlayers.put(String.valueOf(numPlayer) + "hand", tempListHand);
		decksPlayers.put(String.valueOf(numPlayer) + "deck", tempListDeck);
		
		hand.removeAllViews();
		deck.removeAllViews();

	}
	
	private void loadDecksAndHands() {
		if(getIntent().getExtras() != null) {
			Bundle extra = (Bundle) getIntent().getExtras().get("EXTRA");
			if(extra != null) {
				handsPlayers = new HashMap<String, List<View>>();
				decksPlayers = new HashMap<String, List<View>>();
				
				options = (Options) extra.get("OPTIONS");
				numPlayers = options.getNum_Players()-1;
				boardService = new BoardService(this, options);
				
				// TODO CAMBIAR LAS BARAS A VISTA Y GUARDARLAS
				decksPlayers.put("0deck", mapDeck(new Deck(), "Jug0"));
				decksPlayers.put("1deck", mapDeck(new Deck(), "Jug1"));
				decksPlayers.put("2deck", mapDeck(new Deck(), "Jug2"));
				
				loadTurn(0);
				
			} else {
				// No hay opciones
			}
		} else {
			// No hay opciones
		}
	}
	
	private List<View> mapDeck(Deck deck, String prueba) {
		
		List<View> cardViews = new ArrayList<View>();
		List<Card> deckCards = deck.getCards();

		////////////////////// BORRAR ////////////////////// TODO
		List<Card> cards = new ArrayList<Card>();		////
		for (int i = 0; i < 12; i++) {					////
			Card card = new Card();						////
			card.setName(prueba + i);					////
			cards.add(card);							////
		}												////
		////////////////////// BORRAR //////////////////////
		
		for (int i = 0; i < 6; i++) {
			CardView cardView = new CardView(this, cards.get(i));
			cardView.setOnTouchListener(new ClickListener());
			cardViews.add(cardView);
		}
		
		return cardViews;
	}
	
}
